package com.easybenefit.UUService.fragment;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;

import com.easybeanfit.UUService.commutill.UUJsonUtil;
import com.easybeanfit.UUService.commutill.UUNetUtill;
import com.easybeanfit.UUService.commutill.UUSharedUtil;
import com.easybeanfit.UUService.commutill.UUToastUtil;
import com.easybeanfit.UUService.config.UUConfig;
import com.easybeanfit.UUService.config.UUDict;
import com.easybenefit.UUService.R;
import com.easybenefit.UUService.activity.CouponsListActivity;
import com.easybenefit.UUService.adapter.CouponsListAdapter;
import com.easybenefit.UUService.model.CouponlistBean;
import com.easybenefit.UUService.model.HttpCouponBean;
import com.easybenefit.UUService.model.MerchantsInfoBean;
import com.easybenefit.UUService.model.UserInfo;
import com.easybenefit.UUService.myinterface.IActivityInterface;
import com.easybenefit.UUService.xlist.XListView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 46595 on 2016/6/11.
 */
public class CouponsListFragment extends Fragment implements IActivityInterface, View.OnClickListener {
    private View contentView;
    private CouponsListAdapter couponsListAdapter;
    private XListView xListView;
    private Button btn_back;
    private List<CouponlistBean> list;
    private int page = 1;
    private int rows = 10;
    private Context context;
    private FrameLayout flt_top;
    private MerchantsInfoBean merchantsInfoBean;
    private String id;

    public CouponsListFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        contentView = inflater.inflate(R.layout.activity_distributed_coupons_list, container, false);
        initView();
        initData();
        initListener();
        return contentView;
    }

    public static CouponsListFragment newInstance() {
        CouponsListFragment fragment = new CouponsListFragment();
        fragment.setRetainInstance(true);
        return fragment;
    }

    @Override
    public void initData() {
        context = getActivity();
        list = new ArrayList<CouponlistBean>();
        couponsListAdapter = new CouponsListAdapter(list, context, 3);//商户列表无是否选入U卡券
        xListView.setAdapter(couponsListAdapter);
        getCoupData(id);
    }

    @Override
    public void initView() {
        xListView = (XListView) contentView.findViewById(R.id.xlist_coupons);
        xListView.setPullLoadEnable(false);
        xListView.setPullRefreshEnable(false);
        flt_top = (FrameLayout) contentView.findViewById(R.id.flt_top);
        flt_top.setVisibility(View.GONE);
    }

    /**
     * 加载结束需要执行的方法
     */
    public void onload() {
        xListView.stopRefresh();
        xListView.stopLoadMore();
        xListView.setRefreshTime();
    }

    @Override
    public void initListener() {

        xListView.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                page = 1;

                onload();
            }

            @Override
            public void onLoadMore() {
                page++;

                onload();
            }
        });
    }

    @Override
    public void onClick(View v) {

    }

    public MerchantsInfoBean getMerchantsInfoBean() {
        return merchantsInfoBean;
    }

    public void setMerchantsInfoBean(MerchantsInfoBean merchantsInfoBean) {
        this.merchantsInfoBean = merchantsInfoBean;
    }
    /**
     * 查找数据
     */
    public void getCoupData(String mer_id) {

        if (!UUNetUtill.isConnected(context)) {
            UUToastUtil.showToast(context, UUDict.NOT_INTERNET);
        } else {
            /** 获取优惠券列表 */
            String url = UUConfig.SERVER_BASE_URL + "newcoulist";
            RequestParams params = new RequestParams();
            params.put("mer_id", mer_id);
            AsyncHttpClient client = new AsyncHttpClient();
            client.post(url, params, new AsyncHttpResponseHandler() {
                @Override
                public void onStart() {

                }

                @Override
                public void onFinish() {
                    onload();
                }

                @Override
                public void onSuccess(int i, Header[] headers, byte[] bytes) {
                    String content = new String(bytes);
                    HttpCouponBean httpCouponBean = (HttpCouponBean) UUJsonUtil.fromJson(content, HttpCouponBean.class);
                    list.addAll(httpCouponBean.getData().getCouponlist());
                    couponsListAdapter.notifyDataSetChanged();
                }

                @Override
                public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {

                }
            });

        }
    }
    public void setId(String id) {
        this.id = id;
    }
}
